<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Customer extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Customermodel','m_customer',true);
    }

    public function index(){
        $data = parent::index();

        global $g_rental_devices;
        $data['attr'] = array();
        $data['attr']['rental_devices'] = $g_rental_devices;

        $this->load->view('admin/customer',$data);
    }

    public function getCustomerList(){
        $assist['where'] = getFilter($this->input->post(),Customermodel::$searchField);
        $assist['like'] = getFilter($this->input->post(),Customermodel::$likeField);
        $assist['sort'] = getSort($this->input->post(),Customermodel::$sortField);
        $assist['pagination'] = getPagination($this->input->post());

        list($list,$count) = $this->m_customer->getCustomerListSection($assist);

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function getCustomerRentalList() {
      $assist['where'] = getFilter($this->input->post(),Customermodel::$searchField);
      $assist['like'] = getFilter($this->input->post(),Customermodel::$likeField);
      $assist['sort'] = getSort($this->input->post(),Customermodel::$sortField);
      $assist['pagination'] = getPagination($this->input->post());

      list($list,$count) = $this->m_customer->getCustomerRentalListSection($assist);

      $this->response->setSuccess();
      $this->response->setItems($list);
      $this->response->setTotalCount($count);
      $this->response->responseExit();
    }


	public function disableCustomer(){
		$this->validate->setRule('customer_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['customer_status'] = STATUS_DISABLE;

		$this->m_customer->editCustomer($info);


		$this->response->setSuccess();
		$this->response->responseExit();
	}

	public function activeCustomer(){
		$this->validate->setRule('customer_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['customer_status'] = STATUS_ACTIVE;

		$this->m_customer->editCustomer($info);


		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

  // public function deleteCustomer(){
	// 	$this->validate->setRule('customer_id','integer');
  //   if(!$this->validate->run($this->input->post())){
  //     $this->response->setDetail($this->validate->getMessage());
  //     $this->response->responseExit();
  //   }
  //   $info = $this->validate->getResult();
  //   $info['user_id'] = $this->m_app->getCurrentUserId();
  //   $info['customer_status'] = STATUS_DELETE;
  //
  //   $this->m_customer->editCustomer($info);
  //
  //
  //   $this->response->setSuccess();
  //   $this->response->responseExit();
  // }

  public function resetCustomerPassword(){
    $this->validate->setRule('customer_id','integer');
    if(!$this->validate->run($this->input->post())){
      $this->response->setDetail($this->validate->getMessage());
      $this->response->responseExit();
    }
    $info = $this->validate->getResult();

    $this->load->database();
    $this->m_customer->editCustomer(array(
      'customer_id' => $info['customer_id'],
      'customer_password' => md5(DEFAULT_PASSWORD.SALT),
    ));
    $this->db->close();

    $this->response->setSuccess();
    $this->response->responseExit();
  }
}
/* End of file api_log.php */
/* Location: ./application/controllers/api_log.php */
